'''
Date: 2023-12-04 10:59:35
Author: WT-W-PC
LastEditors: WT-W-PC
LastEditTime: 2023-12-07 17:08:23
Description: 
获取最优链接，获取失败则等待一段指数时间后再试
登陆
登陆后获取剩余天数、剩余流量以及优惠活动等信息
签到并格式化返回消息
推送结果
'''

from notify import Message
from aliyunpanrobot import AliyunpanRobot
import utils
from utils import JsonConfig
import math


CONFIG_DIR = 'aliyunpan.json'
ALIYUNPAN_TOKEN_NAME = 'aliyunpan_token'
BARK_KEY_NAME = 'bark_key'

def wait():
  log_msg = f'休息一会...'
  utils.logging.info(log_msg)
  utils.pause()

# 检查环境变量是否为空
def inspectenv(inspection_dict):
    result = True
    details = ''
    for key, value in inspection_dict.items():
        if value == None:
            result = False
            details = details + f'{key}配置为空，'
    if not result:
        details = details + '请检查配置文件。'
    return result, details
        
        
if __name__ == "__main__":
    # 初始化环境信息
    log_msg = f'初始化环境信息...'
    utils.logging.info(log_msg)
    config = JsonConfig(CONFIG_DIR)
    if config.init():
        bark_key = config.read(BARK_KEY_NAME)      
        aliyunpan_token = config.read(ALIYUNPAN_TOKEN_NAME)
        
        waiting_dict = {
            ALIYUNPAN_TOKEN_NAME: aliyunpan_token
        }
        
        # 初始化环境信息
        log_msg = f'获取配置信息...'
        utils.logging.info(log_msg)
        next_step, details = inspectenv(waiting_dict)
        
        # 配置文件不齐全
        if not next_step:
            utils.logging.error(details)
            # 创建bark通知
            if bark_key != None:
                temp_msg = f'创建Bark通知实例...'
                utils.logging.info(temp_msg)
                correspondent = Message(bark_key)
                log_msg = '阿里云盘签到：' + details
                correspondent.push(log_msg)
        else:            
            # 创建juziyun实例
            log_msg = f'创建aliyunpan实例...'
            utils.logging.info(log_msg)
            aliyunpan = AliyunpanRobot(aliyunpan_token)
            
            try:
                log_msg = f'开始阿里云盘认证...'
                utils.logging.info(log_msg)
                aliyunpan.auth()
                wait()
                
                log_msg = f'开始阿里云盘签到...'
                utils.logging.info(log_msg)
                aliyunpan.signin()
                wait()
                
                log_msg = f'开始领取今日奖励...'
                utils.logging.info(log_msg)
                aliyunpan.receiveaward()
                wait()
                
                log_msg = f'更新配置文件信息...'
                utils.logging.info(log_msg)
                aliyunpan_refresh_token = {
                    ALIYUNPAN_TOKEN_NAME: aliyunpan.refresh_token
                }
                config.write(aliyunpan_refresh_token)
                        
                # 通知用户结果
                if bark_key != None:
                    temp_msg = f'创建Bark通知实例...'
                    utils.logging.info(temp_msg)
                    correspondent = Message(bark_key)
                    
                    today_date = utils.gettodaydate()
                    if today_date == str(aliyunpan.sign_in_count):
                        additional_msg = '本月暂时满签，请继续保持。'
                    else:
                        missed_days = abs(int(today_date) - int(aliyunpan.sign_in_count))
                        additional_msg = f'本月漏签 {str(missed_days)} 天，请注意补签。'
                    log_msg = f'阿里云盘签到：今日签到成功，{aliyunpan.today_reward}\n本月累计签到 {aliyunpan.sign_in_count} 天。\n{additional_msg}'
                    
                    correspondent.push(log_msg)
                    
                log_msg = 'juziyun签到完成！'
                utils.logging.info(log_msg)
  
            except Exception as e:
                utils.logging.error(e)
                if isinstance(e, utils.CommonException) and bark_key != None:
                    temp_msg = f'创建Bark实例并通知用户...'
                    utils.logging.info(temp_msg)
                    correspondent = Message(bark_key)
                    log_msg = '阿里云盘签到：' + str(e)
                    correspondent.push(log_msg)                 
    else:
        log_msg = f'初始化环境信息失败，请检查系统环境。'
        utils.logging.error(log_msg)